﻿using BySoft.Server.Business;
using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class QueryMachineByProcess : BasePage
    {

        public DataTable ScheduleMachineDT
        {
            set { Session["ScheduleMachineDT"] = value; }
            get
            {
                if (Session["ScheduleMachineDT"] == null)
                {
                    return CreatMachineDt();
                }
                else
                {
                    return (DataTable)Session["ScheduleMachineDT"];
                }
            }
        }
        public DataTable CreatMachineDt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ScheduleMachineID");
            dt.Columns.Add("ScheduleProcessID");
            dt.Columns.Add("BillDate");
            dt.Columns.Add("ProcessID");
            dt.Columns.Add("MachineID");
            dt.Columns.Add("ExpectInputTime");
            dt.Columns.Add("SingleStandardProcessTime");
            dt.Columns.Add("AdjustTime");
            dt.Columns.Add("SingleClampTime");
            dt.Columns.Add("ExpectProduceTime");
            return dt;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                txtProcessID.Text = UIHelper.GetQueryStringValue("processid");
                hdScheduleProcessID.Value = UIHelper.GetQueryStringValue("scheduleprocessid");
                hdScheduleID.Value = UIHelper.GetQueryStringValue("scheduleid");
                BindDDL();
                cpBillDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                BindData();
            }
            txtProcessID.Attributes.Add("ReadOnly", "true");
            txtStandardTime.Attributes.Add("ReadOnly", "true");
            txtLastStandard.Attributes.Add("ReadOnly", "true");
        }

        private void BindData()
        {
            if (!string.IsNullOrEmpty(txtProcessID.Text.Trim()))
            {
                DataTable dt = ScheduleMachineDT.Copy();
                //查询此工序之前是否已做过
                DataRow[] drs = dt.Select("ProcessID='" + txtProcessID.Text.Trim() + "'");
                if (drs.Length > 0)
                {
                    DataRow dr = drs[0];
                    cpBillDate.Value = string.Format("{0:yyyy-MM-dd}", dr["BillDate"]);
                    txtProcessID.Text = CommonMethod.GetString(dr["ProcessID"]);
                    ddlMachineID.SelectedValue = CommonMethod.GetString(dr["MachineID"]);
                    txtExpectInputTime.Text = CommonMethod.GetString(dr["ExpectInputTime"]);
                    //txtStandardTime.Text = CommonMethod.GetString(dr["StandardTime"]);
                    //txtLastStandard.Text = CommonMethod.GetString(dr["LastStandard"]);
                    txtSingleStandardProcessTime.Text = CommonMethod.GetString(dr["SingleStandardProcessTime"]);
                    txtAdjustTime.Text = CommonMethod.GetString(dr["AdjustTime"]);
                    txtSingleClampTime.Text = CommonMethod.GetString(dr["SingleClampTime"]);
                    cpExpectProduceTime.Value = string.Format("{0:yyyy-MM-dd}", dr["ExpectProduceTime"]);
                    hdScheduleMachineID.Value = CommonMethod.GetString(dr["ScheduleMachineID"]);
                    BindMachineInfo();
                }
            }
        }

        private void BindDDL()
        {
            DataTable dt = BsScheduleBusniess.GetMachineDataTable();
            ddlMachineID.DataTextField = "MachineName";
            ddlMachineID.DataValueField = "MachineID";
            ddlMachineID.DataSource = dt;
            ddlMachineID.DataBind();
            UIHelper.InsertFirstItem(ddlMachineID);
        }


        public void BindMachineInfo()
        {
            txtStandardTime.Text = "";
            txtLastStandard.Text = "";
            if (string.IsNullOrEmpty(cpBillDate.Value.Trim()))
            {
                UIHelper.AlertMessage(this.Page, "请先选择日期！");
                return;
            }
            decimal TotalExpectInputTime = 0;//指定时间，其他工序用此机台的总时间
            //机台的信息
            MdMachineEntity en = MdMachine.GetMdMachineByID(ddlMachineID.SelectedValue.Trim());
            decimal StandardTime = 0;
            if (en != null)
            {
                txtStandardTime.Text = en.StandardTime.ToString();
                StandardTime = CommonMethod.ConvertToDecimal(en.StandardTime);
            }

            //取当前排程内所用到的此机台的数据
            DataTable dt = ScheduleMachineDT.Copy();
            DataRow[] drs = dt.Select("BillDate='" + cpBillDate.Value.Trim() + "' and MachineID='" + ddlMachineID.SelectedValue.Trim() + "' and ProcessID <> '" + txtProcessID.Text.Trim() + "'");
            foreach (DataRow dr in drs)
            {
                TotalExpectInputTime += CommonMethod.ConvertToDecimal(dr["ExpectInputTime"]);
            }

            //取其他排程内所用到的此机台的数据
            DataTable detail = BsScheduleMachineBusiness.GetDTByCondition(cpBillDate.Value.Trim(), ddlMachineID.SelectedValue.Trim(), hdScheduleMachineID.Value.Trim(), hdScheduleProcessID.Value.Trim(), hdScheduleID.Value.Trim());
            foreach (DataRow dr in detail.Rows)
            {
                TotalExpectInputTime += CommonMethod.ConvertToDecimal(dr["ExpectInputTime"]);
            }

            //剩余产能时间=机台标准产能时间-指定时间,排程内所用到的此机台的时间-当前预计使用时间
            txtLastStandard.Text = (Math.Round(StandardTime - TotalExpectInputTime 
                //- CommonMethod.ConvertToDecimal(txtExpectInputTime.Text)
                , 2)).ToString();
        }
        protected void ddlMachineID_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindMachineInfo();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            decimal LastStandard = CommonMethod.ConvertToDecimal(txtLastStandard.Text);
            if (LastStandard < 0)
            {
                UIHelper.AlertMessage(this.Page, "此机台超负荷，请重新选择！");
                return;
            }
            DataTable dt = ScheduleMachineDT.Copy();
            //查询此工序是否存在
            DataRow[] drs = dt.Select("ProcessID='" + txtProcessID.Text.Trim() + "'");
            if (drs.Length > 0)
            {
                DataRow dr = drs[0];
                dr["BillDate"] = cpBillDate.Value.Trim();
                dr["ProcessID"] = txtProcessID.Text.Trim();
                dr["MachineID"] = ddlMachineID.SelectedValue.Trim();
                dr["ExpectInputTime"] = txtExpectInputTime.Text.Trim();
                dr["SingleStandardProcessTime"] = txtSingleStandardProcessTime.Text.Trim();
                dr["AdjustTime"] = txtAdjustTime.Text.Trim();
                dr["SingleClampTime"] = txtSingleClampTime.Text.Trim();
                dr["ExpectProduceTime"] = cpExpectProduceTime.Value.Trim();
                dr["ScheduleMachineID"] = hdScheduleMachineID.Value.Trim();
            }
            else
            {
                DataRow dr = dt.NewRow();
                dr["BillDate"] = cpBillDate.Value.Trim();
                dr["ProcessID"] = txtProcessID.Text.Trim();
                dr["MachineID"] = ddlMachineID.SelectedValue.Trim();
                dr["ExpectInputTime"] = txtExpectInputTime.Text.Trim();
                dr["SingleStandardProcessTime"] = txtSingleStandardProcessTime.Text.Trim();
                dr["AdjustTime"] = txtAdjustTime.Text.Trim();
                dr["SingleClampTime"] = txtSingleClampTime.Text.Trim();
                dr["ExpectProduceTime"] = cpExpectProduceTime.Value.Trim();
                dr["ScheduleMachineID"] = hdScheduleMachineID.Value.Trim();
                dr["ScheduleProcessID"] = "-1";
                dt.Rows.Add(dr);
            }
            ScheduleMachineDT = dt.Copy();
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'success');CloseTopDialog();</script>");
        }

        protected void btnBillDate_Click(object sender, EventArgs e)
        {
            BindMachineInfo();
        }
    }
}